package com.lizhaoshan.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.StrUtil;
import com.lizhaoshan.dao.CategoryDao;
import com.lizhaoshan.dao.SetMealDao;
import com.lizhaoshan.dao.SetMealDishDao;
import com.lizhaoshan.dto.SetMealDto;
import com.lizhaoshan.pojo.*;
import com.lizhaoshan.service.SetMealService;
import com.lizhaoshan.vo.SetMealVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @program: reggie
 * @description:
 * @author: lamour
 * @create: 2022-05-14 17:19
 **/

@Slf4j
@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealDao setMealDao;

    @Autowired
    private SetMealDishDao setMealDishDao;

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public boolean addSetMeal(SetMealDto setMealDto){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String format = dtf.format(LocalDateTime.now());

        String idStr = new Snowflake().nextIdStr();
        setMealDto.setId(idStr);

        setMealDto.setUpdateUser(setMealDto.getId());
        setMealDto.setCreateUser(setMealDto.getId());

        setMealDto.setCreateTime(format);
        setMealDto.setUpdateTime(format);
        setMealDto.setIsDeleted(0);

        SetMeal setMeal = new SetMeal();
        BeanUtils.copyProperties(setMealDto, setMeal);

        List<SetMealDish> setMealDishes = setMealDto.getSetmealDishes();

        List<SetMealDish> setMealDishList = new ArrayList<>();

        for (SetMealDish setMealDish : setMealDishes) {
            SetMealDish mealDish = new SetMealDish();
            BeanUtils.copyProperties(setMealDish, mealDish);

            String id = new Snowflake().nextIdStr();
            setMealDish.setId(id);

            setMealDish.setSetMealId(idStr);
            setMealDish.setSort(1);
            setMealDish.setCreateUser(setMealDto.getCreateUser());
            setMealDish.setUpdateUser(setMealDto.getUpdateUser());
            setMealDish.setCreateTime(format);
            setMealDish.setUpdateTime(format);
            setMealDish.setIsDeleted(0);

            setMealDishList.add(setMealDish);
        }

        // if (CollUtil.isNotEmpty(setMealDishes)){
        //     for (SetMealDish setMealDish : setMealDishes) {
        //         if(Objects.nonNull(setMealDish)){
        //             String DishId = new Snowflake().nextIdStr();
        //             setMealDish.setId(DishId);
        //             setMealDish.setCreateUser(setMealDto.getId());
        //             setMealDish.setUpdateUser(setMealDto.getId());
        //             setMealDish.setIsDeleted(0);
        //         }
        //     }
        // }
        int insertSetMeal = setMealDao.insertSetMeal(setMeal);
        int insertSetMealDish = setMealDishDao.insertSetMealDish(setMealDishList);


        return insertSetMeal > 0 && insertSetMealDish == setMealDishList.size() ;
    }

    @Override
    public boolean putSetMeal(SetMealDto setMealDto) {
        //获取dish的id,用于判断该id的dish是否存在，并将创建人和创建时间赋值给口味
        String id = setMealDto.getId();

        log.info("********通过id查找数据库中的setMeal，begin");
        SetMeal setMeal = setMealDao.selectById(id);
        log.info("********通过id查找数据库中的setMeal，end");
        if (Objects.isNull(setMeal)) {
            return false;
        }
        //用户可能新增套餐菜品，所以不能仅仅修改套餐菜品
        log.info("********通过setMeal_Id删除套餐菜品，重新添加的方式代替修改");
        int deleteById = setMealDishDao.deleteBySetMealId(id);
        log.info("********删除套餐菜品，end");

        List<SetMealDish> setMealDishList = new ArrayList<>();

        //获取前端传进来的套餐菜品，进行判空
        List<SetMealDish> setmealDishes = setMealDto.getSetmealDishes();
        if (CollUtil.isEmpty(setmealDishes)) {
            return false;
        }
        for (SetMealDish setmealDish : setmealDishes) {
            if (Objects.isNull(setmealDish)){
                continue;
            }
            //设置用户id
            String sdId = new Snowflake().nextIdStr();
            setmealDish.setId(sdId);
            setmealDish.setSetMealId(id);
            //设置修改时间
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
            String format = dtf.format(LocalDateTime.now());
            setmealDish.setCreateTime(format);
            setmealDish.setUpdateTime(format);
            //设置修改用户
            setmealDish.setUpdateUser(setMeal.getUpdateUser());
            setmealDish.setCreateUser(setMeal.getCreateUser());
            //设置是否删除
            setmealDish.setIsDeleted(0);

            //设置sort值
            setmealDish.setSort(1);

            SetMealDish setMealDish = new SetMealDish();
            BeanUtils.copyProperties(setmealDish, setMealDish);
            setMealDishList.add(setMealDish);

        }

        //设置setmeal的dish属性
        setMealDto.setSetmealDishes(setmealDishes);

        //批量添加套餐菜品表
        log.info("**********批量添加套餐菜品，begin");
        int insertSetMealDish = setMealDishDao.insertSetMealDish(setMealDishList);
        log.info("**********批量添加套餐菜品，end");

        //修改套餐
        SetMeal setMeal1 = new SetMeal();
        BeanUtils.copyProperties(setmealDishes, setMeal1);
        log.info("**********修改套餐，begin");
        int update = setMealDao.update(setMeal1);
        log.info("**********修改套餐，end");



        return deleteById == setMealDishList.size() && insertSetMealDish == setMealDishList.size() && update > 0;

    }

    @Override
    public PageBean<SetMealVo> getSetMealPage(Integer page, Integer pageSize, String name) {
        int begin=(page-1)*pageSize;
        int size=pageSize;

        if (name != null && name.length() > 0) {
            name="%"+name+"%";
        }

        int count = setMealDao.count();

        log.info("********查询setmeal");
        List<SetMealVo> setMealVoList = setMealDao.selectPage(begin, size, name);
        log.info("********查询setmeal，end");

        if (CollUtil.isEmpty(setMealVoList)){
            return null;
        }

        for (SetMealVo setMealVo : setMealVoList) {
            if (Objects.isNull(setMealVo)){
                continue;
            }
            String setMealId = setMealVo.getId();
            log.info("*******通过setMealId查询setMealDish表，begin");
            List<SetMealDish> setMealDishList = setMealDishDao.selectBySetMealId(setMealId);
            log.info("*******通过setMealId查询setMealDish表，end");
            if(CollUtil.isEmpty(setMealDishList)){
                setMealVo.setSetMealDish(setMealDishList);
            }
        }

        PageBean<SetMealVo> setMealVoPageBean = new PageBean<>();
        setMealVoPageBean.setRecords(setMealVoList);
        setMealVoPageBean.setCurrent(count);

        return setMealVoPageBean;
    }

    @Override
    public boolean deleteByIds(String[] ids) {
        if (Objects.isNull(ids)){
            return false;
        }

        //数组转集合
        List<String> stringList = Arrays.asList(ids);

        //TODO 查询套餐，查看套餐状态，如果套餐正在售卖中，不能删除 ，记录可以删除的套餐id用于 setmealDish的删除
        int deleteByIds = setMealDao.deleteByIds(ids);

        for (String id : stringList) {
            int deleteBySetMealId = setMealDishDao.deleteBySetMealId(id);
            int countBySetMealId = setMealDishDao.countBySetMealId(id);
            if(deleteBySetMealId != countBySetMealId){
                return false;
            }
        }


        return deleteByIds == ids.length;
    }

    @Override
    public boolean updateStatus(Integer status, String[] ids) {

        if (Objects.isNull(ids) && Objects.isNull(status)){
            return false;
        }

        //数组转集合
        List<String> stringList = Arrays.asList(ids);
        int updateStatus = setMealDao.updateStatus(status, stringList);

        return updateStatus == stringList.size();
    }

    @Override
    public SetMealVo getSetMealById(String id) {
        if(StrUtil.isEmpty(id)){
            return null;
        }
        SetMeal setMeal = setMealDao.selectById(id);
        List<SetMealDish> setMealDishList = setMealDishDao.selectBySetMealId(id);

        String categoryName = null;
        Integer type = 1;
        if (Objects.nonNull(setMeal)){
            String categoryId = setMeal.getCategoryId();
            Category category = categoryDao.selectById(categoryId);
            if (Objects.nonNull(category)){
                categoryName = category.getName();
                type = category.getType();
            }
        }

        SetMealVo setMealVo = new SetMealVo();
        setMealVo.setType(type);
        setMealVo.setCategoryName(categoryName);
        setMealVo.setSetMealDish(setMealDishList);

        BeanUtils.copyProperties(setMeal,setMealVo);

        return setMealVo;
    }


}
